﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Windows.Navigation;
using System.Collections.Generic;
using System.Linq;

using Visifire.Charts;

using SalePoints.Views;
using SalePoints.Utility;
using SalePoints.WcfServices;

using SystemX.Infrastructure;

namespace SalePoints.Models
{
	public class ChartViewModel
	{
		private DataLegendGroup _Legends;
		private SaleChannel _Channels;
		private DispatcherTimer _Timer;
		private ZoomWindow _zWindow;
		private TimeSpan _Interval = new TimeSpan(0, 3, 30);
		private string _Theme = "Theme3";
		private List<Chart> _Charts;
		private Panel _ChartsWrapper;
		private int _RenderType=ChartRenderType.Column;
		private int _YValueOption = SalePointOptions.AMOUNT;
		private NavigationEventArgs _nvEventArg;
		private double _TrendlineWidth = 3;
		private List<SalePointInfo> _salePointItems;
		public Action OnLoadingData { get; set; }
		public Action OnDataLoaded { get; set; }
		public IntervalTypes IntervalType { get; set; }
		public string XValueFormatStr { get; set; }
		public DispatcherTimer Timer {
			get
			{
				return _Timer;
			}
			set
            {
            	_Timer = value;
            }
		}

		/// <summary>
		/// .ctor
		/// </summary>
		/// <param name="legend">统计维</param>
		/// <param name="channels">销售渠道</param>
		/// <param name="trendlineWidth">图表上背景色带宽度</param>
		/// <param name="charts">涉及的图表</param>
		/// <param name="chartWrapper">图表父元素</param>
		/// <param name="refreshInterval">图表刷新频率。单位分钟</param>
		public ChartViewModel(DataLegendGroup legend, SaleChannel channels,double trendlineWidth, List<Chart> charts, Panel chartWrapper,int refreshInterval)
		{
			_Interval = refreshInterval > 0 ? new TimeSpan(0, refreshInterval, 0) : _Interval;

			_Legends = legend;
			_Channels = channels;
			_Timer = new DispatcherTimer();
			_zWindow = new ZoomWindow();
			
			_Charts = charts;
			_ChartsWrapper = chartWrapper;
			_TrendlineWidth = trendlineWidth.Equals(double.NaN)?_TrendlineWidth:trendlineWidth;
		}
		public ChartViewModel(DataLegendGroup legend, SaleChannel channels, List<Chart> charts, Panel chartWrapper, int refreshInterval)
			:this(legend,channels,double.NaN,charts,chartWrapper,refreshInterval)
		{ 
		
		}
		private void OnClickChartTitle(Chart chart)
		{
			_zWindow.Zoom(_ChartsWrapper, chart, Size.Empty);
		}
		public void OnChartTypeChanged(object sender, SelectionChangedEventArgs e) {
			var cbx = sender as ComboBox;
			if (sender == null) return;
			var item = (KeyValuePair<int, string>)cbx.SelectedItem;
			_RenderType = item.Key;
			_Charts.ForEach(x => {
				x.Series.ToList().ForEach(y => { y.RenderAs = ChartHelper.GetRenderType(_RenderType); });
			});
			//OnNavigatedTo(_nvEventArg);
		}
		public void OnThemeChanged(object sender, SelectionChangedEventArgs e)
		{
			var cbx = sender as ComboBox;
			if (sender == null) return;
			var item =cbx.SelectedItem as ComboBoxItem;
			_Theme = item.Content.ToString();
			_Charts.ForEach(x => { x.Theme = _Theme; });
			//OnNavigatedTo(_nvEventArg);
		}
		public void OnYValueOptionChange(object sender, SelectionChangedEventArgs e) {
			var cbx = sender as ComboBox;
			if (sender == null) return;
			var item = (KeyValuePair<int, string>)cbx.SelectedItem;
			_YValueOption = item.Key;
			OnNavigatedTo(_nvEventArg);
		}
		public void OnNavigatedTo(NavigationEventArgs e) {
			_nvEventArg = e;
			//_Charts.InitCharts(_Legends, OnClickChartTitle, _TrendlineWidth, _RenderType, IntervalType, XValueFormatStr);
			autoRefresh();
		}
		void autoRefresh() {
			_Timer.Stop();
			_Timer.Tick -= _Timer_Tick;
			_Timer.Tick += _Timer_Tick;
			_Timer.Interval = _Interval;
			_Timer.Start();
			//firstly run
			_Timer_Tick(null, new EventArgs());
		}

		void _Timer_Tick(object sender, EventArgs e)
		{
			if (OnLoadingData != null) {
				OnLoadingData();
			}
			SalePoint.LoadData(_Legends, _Channels, items =>
			{
				if (OnDataLoaded != null) { OnDataLoaded(); };
				_salePointItems = items;
				_Charts.ForEach(x =>
				{
					x.DrawSalePoints(_salePointItems, getTitle(x.Tag.ToString()), OnClickChartTitle, _TrendlineWidth, _RenderType, IntervalType, XValueFormatStr ?? "", false, _YValueOption);
				});
			});
		}
		string getTitle(string tag) {
			var retVal = tag;
			switch (tag) { 
				case "Website":
					retVal = "麦网";
					break;
				case "MailOrder":
					retVal="邮购";
					break;
				case "HealthDep":
					retVal = "健康";
					break;
				case "Store":
					retVal = "门店";
					break;
			}
			return retVal;
		}
	}
}
